package com.team.service.controller;

import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.api.model.v2.Result;
import com.team.service.domain.dto.ServiceItemAddDto;
import com.team.service.domain.dto.ServiceItemDto;
import com.team.service.domain.vo.ServiceItemVo;
import com.team.service.generator.RedisIdGenerator;
import com.team.service.util.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.team.common.log.annotation.Log;
import com.team.common.log.enums.BusinessType;
import com.team.common.security.annotation.RequiresPermissions;
import com.team.service.domain.ServiceItem;
import com.team.service.service.IServiceItemService;
import com.team.common.core.web.controller.BaseController;
import com.team.common.core.web.domain.AjaxResult;
import com.team.common.core.utils.poi.ExcelUtil;
import com.team.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务管理Controller
 * 
 * @author panyue
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/manage")
public class ServiceItemController extends BaseController
{
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired private RedisIdGenerator idGenerator;

    /**
     * 查询服务管理列表
     */
    @RequiresPermissions("manage:manage:list")
    @GetMapping("/list")
    public TableDataInfo list(ServiceItemDto serviceItemDto)
    {
        startPage();
        List<ServiceItemVo> list = serviceItemService.selectServiceItemList(serviceItemDto);
        return getDataTable(list);
    }

    /**
     * 导出服务管理列表
     */
    @RequiresPermissions("manage:manage:export")
    @Log(title = "服务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceItemDto serviceItem)
    {
        List<ServiceItemVo> list = serviceItemService.selectServiceItemList(serviceItem);
        ExcelUtil<ServiceItemVo> util = new ExcelUtil<ServiceItemVo>(ServiceItemVo.class);
        util.exportExcel(response, list, "服务管理数据");
    }

    /**
     * 获取服务管理详细信息
     */
    @RequiresPermissions("manage:manage:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(serviceItemService.selectServiceItemById(id));
    }

    /**
     * 新增服务管理
     */
    @RequiresPermissions("manage:manage:add")
    @Log(title = "服务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceItemAddDto serviceItemAddDto)
    {
        return toAjax(serviceItemService.insertServiceItem(serviceItemAddDto));
    }

    /**
     * 修改服务管理
     */
    @RequiresPermissions("manage:manage:edit")
    @Log(title = "服务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceItemAddDto serviceItemAddDto)
    {
        return toAjax(serviceItemService.updateServiceItem(serviceItemAddDto));
    }

    /**
     * 删除服务管理
     */
    @RequiresPermissions("manage:manage:remove")
    @Log(title = "服务管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serviceItemService.deleteServiceItemByIds(ids));
    }
    /**
     * 查询商家列表
     */
    @RequiresPermissions("manage:manage:addNewService")
    @GetMapping("/list-merchants")
    public TableDataInfo listMerchant()
    {
        List<String> list = serviceItemService.selectMerchantList();
        return getDataTable(list);
    }
    /**
     * 查询服务类别列表
     */
    @RequiresPermissions("manage:manage:addNewService")
    @GetMapping("/list-categories")
    public TableDataInfo listService(){
        return getDataTable(serviceItemService.selectServices());
    }


    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        try {

            String originalFilename = file.getOriginalFilename();
            String extension= originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString()+extension;
            String objectNameUrl="car/"+objectName;
            String url = aliOssUtil.upload(file.getBytes(), objectNameUrl);
            return Result.success(url);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/services-number")
    public AjaxResult createServiceNumber() {
        try {
            long serviceId = idGenerator.generateId(); // 生成唯一ID
            // 确保返回格式为 {"code":200,"msg":"操作成功","data":"17522060334240002"}
            return success(String.valueOf(serviceId));
        } catch (Exception e) {
            return error("生成服务编号失败: " + e.getMessage());
        }
    }
}
